﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace WW.NActiveRecord.Tests{
    /// <summary>
    /// 特性驱动编程的断言扩展
    /// </summary>
    static class Spec {
        static public IEnumerable<T> ShouldBeJson<T>(this IEnumerable<T> self, string expectedJson) {
             string actual = string.Format("[{0}]", string.Join(",",self.Select(x=>x.ToString()).ToArray()));
             Assert.AreEqual(expectedJson, actual);
             return self;
        }

        static public IEnumerable<T> ShouldBeJson<T>(this IEnumerable<T> self, string expectedJson,string message) {
            string actual = string.Format("[{0}]", string.Join(",", self.Select(x => x.ToString()).ToArray()));
            Assert.AreEqual(expectedJson, actual,message);
            return self;
        }

        static public T ShouldBe<T>(this T self, object expected){
            Assert.AreEqual(expected, self);
            return self;
        }

        static public void ShouldBe(this object self, object expected, string message){
            Assert.AreEqual(expected, self,message);
        }
        
        static public void ShouldNotBe(this object self, object expected) {
            Assert.AreNotEqual(expected,self);
        }

        public static void ShouldNotBe(this object self, object expected, string message) {
            Assert.AreNotEqual(expected,self, message);
        }

        static public void ShouldNull(this object self, string message)
        {
            Assert.IsNull(self, message);
        }
        static public void ShouldNotNull(this object self, string message)
        {
            Assert.IsNotNull(self, message);
        }

        static public void ShouldNull(this object self)
        {
            Assert.IsNull(self);
        }
        static public void ShouldNotNull(this object self)
        {
            Assert.IsNotNull(self);
        }
        /// <summary>
        /// 异常断言工具
        /// </summary>
        /// <param name="type">期望的异常类型</param>
        /// <param name="action">期望失败的action</param>
        public static void ShouldFail(Type type, Action action) {
            try {
                action();
                Assert.Fail("期望的异常没有出现");
            } catch (Exception e) {
                if (!e.GetType().Equals(type) && e.Message != "期望的异常没有出现") {
                    throw new Exception(string.Format("期望出现的异常类型不符合\nExpected: {0}\nBut was:  {1}", type, e.GetType()), e);
                }
                if (e.GetType().Equals(typeof(AssertionException))) {
                    throw;
                }
            }
        }

        /// <summary>
        /// 异常断言工具
        /// </summary>
        /// <param name="action">期望失败的action</param>
        public static void ShouldNotFail(Action action) {
            try {
                action();
            } catch (Exception e) {
                throw new Exception("出现了不期望的异常", e);
            }
        }
    }
}